package com.woniuxy.authapp.app.controller;

import com.nimbusds.jose.jwk.JWKSet;
import com.nimbusds.jose.jwk.RSAKey;
import com.woniuxy.authapp.client.KeyPairClient;
import com.woniuxy.commons.util.Result;
import com.woniuxy.commons.util.WebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.security.KeyPair;
import java.security.interfaces.RSAPublicKey;
import java.util.Map;

@RestController
public class KeyPairController implements KeyPairClient {

    @Autowired
    private KeyPair keyPair;


    @Override
    public Map<String,Object> getKey(){
        //获取公钥强转为RSAPublicKey
        RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
        RSAKey rsaKey = new RSAKey.Builder(rsaPublicKey).build();
        return new JWKSet(rsaKey).toJSONObject();
    }

    @Override
    public void getKey2() {
        HttpServletRequest request = WebUtil.getRequest();
        String requestURI = request.getRequestURI();
        System.out.println(requestURI);
        System.out.println(request.toString());
        System.out.println("1111111");
    }
}
